#include "etcd.hpp"
#include "channel.hpp"
#include "utils.hpp"
#include "chat_session.hxx"
#include <gflags/gflags.h>
#include <gtest/gtest.h>
#include <thread>
#include <boost/date_time/posix_time/posix_time.hpp>
#include "message.pb.h"
#include "base.pb.h"
#include "user.pb.h"
#include "friend.pb.h"
DEFINE_bool(run_mode, false, "程序的运行模式, false-调试； true-发布；");
DEFINE_string(log_file, "", "发布模式下，用于指定日志的输出文件");
DEFINE_int32(log_level, 0, "发布模式下，用于指定日志输出等级");

DEFINE_string(etcd_host, "http://127.0.0.1:2379", "服务注册中心地址");
DEFINE_string(base_service, "/service", "服务监控根目录");
DEFINE_string(friend_service, "/service/friend_service", "关注的服务名称");
ServiceChannel::ChannelPtr channel;
void TestAddFriend() {
    chat_system::FriendAddReq req;
    chat_system::FriendAddRsp rsp;
    req.set_user_id("6919-9f03ea75-0000");
    req.set_respondent_id("id4");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.FriendAdd(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("添加好友失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    LOG_DEBUG("添加好友成功");
}


void TestAddProcess() {
    chat_system::FriendAddProcessReq req;
    chat_system::FriendAddProcessRsp rsp;
    req.set_user_id("id4");
    req.set_apply_user_id("6919-9f03ea75-0000");
    req.set_agree(true);
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.FriendAddProcess(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("处理好友请求失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    LOG_DEBUG("处理好友请求成功"); 
}


void TestSearchUser() {
    chat_system::FriendSearchReq req;
    chat_system::FriendSearchRsp rsp;
    req.set_user_id("6919-9f03ea75-0000");
    req.set_search_key("卜子");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.FriendSearch(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("搜索用户失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    for(const auto& user : rsp.user_info()) {
        LOG_DEBUG("用户信息: {} - {} - {} - {}", user.user_id(), user.nickname(), user.avatar(), user.description());
    }
}


void TestGetFriendList() {
    chat_system::GetFriendListReq req;
    chat_system::GetFriendListRsp rsp;
    req.set_user_id("id1");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.GetFriendList(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("获取好友列表失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    for(const auto& friend_info : rsp.friend_list()) {
        LOG_DEBUG("好友信息: {} - {} - {} - {}", 
                  friend_info.user_id(), 
                  friend_info.nickname(), 
                  friend_info.avatar(), 
                  friend_info.description());
    }
}

void TestFriendRemove() {
    chat_system::FriendRemoveReq req;
    chat_system::FriendRemoveRsp rsp;
    req.set_user_id("e7b5-538ed478-0001");
    req.set_peer_id("id3");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.FriendRemove(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("删除好友失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    LOG_DEBUG("删除好友成功");
}


void TestChatSessionCreate() {
    chat_system::ChatSessionCreateReq req;
    chat_system::ChatSessionCreateRsp rsp;
    req.set_user_id("6919-9f03ea75-0000");
    req.set_chat_session_name("测试群聊");
    req.mutable_member_id_list()->Add("6919-9f03ea75-0000");
    req.mutable_member_id_list()->Add("e7b5-538ed478-0001");
    req.mutable_member_id_list()->Add("id3");
    req.mutable_member_id_list()->Add("id4");
    req.mutable_member_id_list()->Add("id5");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.ChatSessionCreate(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("创建会话失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    LOG_DEBUG("创建会话成功, 会话ID");
}


void TestGetChatSessionMember() {
    chat_system::GetChatSessionMemberReq req;
    chat_system::GetChatSessionMemberRsp rsp;
    req.set_user_id("6919-9f03ea75-0000");
    req.set_chat_session_id("8523-eaa133f5-0002");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.GetChatSessionMember(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("获取会话成员失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    for(const auto& member : rsp.member_info_list()) {
        LOG_DEBUG("成员信息: {} - {} - {} - {}", 
                  member.user_id(), 
                  member.nickname(), 
                  member.avatar(), 
                  member.description());
    }
}


void TestGetChatSessionList() {
    chat_system::GetChatSessionListReq req;
    chat_system::GetChatSessionListRsp rsp;
    req.set_user_id("e7b5-538ed478-0001");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.GetChatSessionList(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("获取会话列表失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    for(const auto& session_info : rsp.chat_session_info_list()) {
        LOG_DEBUG("会话信息: {} - {} - {}", 
                  session_info.chat_session_id(), 
                  session_info.chat_session_name(), 
                  session_info.prev_message().message_id());
    }
}


void TestGetPendingFriendEventList() {
    chat_system::GetPendingFriendEventListReq req;
    chat_system::GetPendingFriendEventListRsp rsp;
    req.set_user_id("id4");
    brpc::Controller cntl;
    chat_system::FriendService_Stub stub(channel.get());
    stub.GetPendingFriendEventList(&cntl, &req, &rsp, nullptr);
    LOG_DEBUG("---{}---{}---", cntl.Failed(), rsp.success());
    if(cntl.Failed() || !rsp.success()) {
        LOG_ERROR("获取待处理好友请求失败: {}---{}---", rsp.errmsg(), cntl.ErrorText());
        return;
    }
    for(const auto& event : rsp.event()) {
        LOG_DEBUG("好友请求事件 --- {} ---", event.sender().DebugString());
    }
}



int main(int argc, char* argv[]) {
    init_logger(FLAGS_log_file, int_log_level_map(FLAGS_log_level), FLAGS_run_mode);
    google::ParseCommandLineFlags(&argc, &argv, true);
    testing::InitGoogleTest();
    ServiceManager::ptr service_manager = std::make_shared<ServiceManager>();
    service_manager->declared(FLAGS_friend_service);
    auto put_cb = std::bind(&ServiceManager::onServiceOnline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    auto del_cb = std::bind(&ServiceManager::onServiceOffline, service_manager.get(), std::placeholders::_1, std::placeholders::_2);
    Discovery::ptr dclient = std::make_shared<Discovery>(FLAGS_etcd_host, FLAGS_base_service, put_cb, del_cb);
    //获取好友服务信道管理对象
    auto channel_manager = service_manager->choose(FLAGS_friend_service);
    if(channel_manager == nullptr) {
        LOG_DEBUG("获取文件服务信道失败");
        return -1;
    }
    // 获取用户服务信道
    channel = channel_manager->choose();
    if(channel == nullptr) {
        LOG_DEBUG("获取用户服务信道失败");
        return -1;
    }
    // TestAddFriend();
    TestAddProcess();
    // TestSearchUser();  
    // TestGetFriendList();
    // TestFriendRemove();
    // TestChatSessionCreate();
    // TestGetChatSessionMember();
    // TestGetChatSessionList();
    TestGetPendingFriendEventList();
    return 0;
}